/*
// drop : drag and drop style interface implementation function
// Version : 0.02
// Written by: Noah Peters
//
Copyright (c) 2008, Noah Peters
All rights reserved.
Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:
        ¥       Redistributions of source code must retain the above 
        copyright notice, this list of conditions and the following 
        disclaimer.
        ¥       Redistributions in binary form must reproduce the above 
        copyright notice, this list of conditions and the following 
        disclaimer in the documentation and/or other materials provided 
        with the distribution.
        ¥       Neither the name of the <ORGANIZATION> nor the names of its 
        contributors may be used to endorse or promote products derived 
        from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
//
*/

var ob, X, Y, Ox, Oy, Ow, Oh, Oz;
var mode;
var obs=new Array;
var O=new Array;
var ds=true;
pf=0.2;
function setMode(m) {
	var b=$(mode);
	if (b)
		b.style.color='white';

	mode = m;

	var b=$(mode);
	if (b)
		b.style.color='orange';

}
function mo(name, x, y, h, w, a, z) {
	this.name=name;
	this.x=x;
	this.y=y;
	this.h=h;
	this.w=w;
	this.a=a;
	this.z=z
}
function MD(e) {
	ds=false;
	var id="";
	if (e) {
		X=e.clientX
		Y=e.clientY
		try {
			Ox=e.target.offsetLeft;
			Oy=e.target.offsetTop;
			Oh=e.target.offsetHeight;
			Ow=e.target.offsetWidth;
			id=e.target.id;
			Oz=e.target.style.zIndex;
		} catch (er) { }
	} else {
		X=event.clientX;//event.offsetX;
		Y=event.clientY;//event.offsetY;
		try {
			Ox=event.srcElement.offsetLeft;
			Oy=event.srcElement.offsetTop;
			Oh=event.srcElement.offsetHeight;
			Ow=event.srcElement.offsetWidth;
			id=event.srcElement.id;
			Oz=event.srcElement.style.zIndex;
		} catch (er) { }
	}
	if (mode=='measure') {
		startMeasure();
		ob=meas;
		window.status=ob.id;
	} else { //regular modes
		//window.status=X + ', ' + Y;
		if (document.layers) {
			ob = document.layers[e.target.id];
		} else if (e && e.target) {
			ob = $(e.target.id);
		} else {
			ob = event.srcElement;
		}
	}
	if (!O[id]) {
		O[id]=new mo(id, Ox, Oy, Oh, Ow, 0, Oz);
	}
	if (mode=='delete' && ob && ob.className=='instance') {
		var time = parseInt($('et').value);
		var fps = parseInt($('ef').value);
		var s = ob.style;				
		var t = (s.pixelTop) ? s.pixelTop : parseInt(s.top.match(/(\d+)/)[1]);
		var l = (s.pixelLeft) ? s.pixelLeft : parseInt(s.left.match(/(\d+)/)[1]);
		var w = (s.pixelWidth) ? s.pixelWidth : parseInt(s.width.match(/(\d+)/)[1]);
		var h = (s.pixelHeight) ? s.pixelHeight : parseInt(s.height.match(/(\d+)/)[1]);
		
		fade(ob,null,time,null,null,fps);	
		move(ob,null,time,l,t,l+w/2,t+h/2,fps);
		size(ob,deleteObj,time,w,h,0,0,fps);
		
		O[ob.id]=null;
		setMode('move');
	}
	//return false;
}
function deleteObj(ob) {
	if (ob.parentElement) {
		ob.parentElement.removeChild(ob);
	} else {
		ob.parentNode.removeChild(ob);
	}
}
function MM(e) {
	//alert('b');
	if (ob) {
		if (ob.className=='template') {
			HD(ob);
			//alert('template');
			if (!obs[ob.id]) {obs[ob.id]=0;}
			obs[ob.id]=obs[ob.id]+1;
			ob = ob.cloneNode(true);
			ob.style.top=Oy;
			ob.style.left=Ox;
			ob.style.width=Ow;
			ob.style.height=Oh;
			ob.style.zIndex=100;
			ob.className='instance';
			O[ob.id + obs[ob.id]]=O[ob.id];
			ob.id=ob.id + obs[ob.id];
			window.status=ob.id;
			document.body.appendChild(ob);
			ds=true;
			SD(ob);
			ds=false;
		}
		var nX,nY,dX,dY;
		if (!e) {
			dX=event.clientX-X;
			dY=event.clientY-Y;
			nX=dX + Ox;//document.body.scrollLeft;
			nY=dY + Oy;//document.body.scrollTop;
		} else {
			dX=e.clientX - X;
			dY=e.clientY - Y;
			nX=dX + Ox;
			nY=dY + Oy;
		}
		//window.status=nX + ', ' + nY;
		if (mode=='size' || mode=='measure') {
			HD(ob);
			window.status=dX + ', ' + dY + ' ' + Ow + ', ' + Oh;
			//return false;
			try {
				ob.style.width=dX + Ow;
				ob.style.height=dY + Oh;
			} catch (er) { }
			nX=nX-dX;//2;
			nY=nY-dY;//2;
			ds=true;
			SD(ob);
			ds=false;
		}
		if (mode=='move'){
			window.status='moving ' + nX + ' ' + nY
			if (ob.moveTo) {
				ob.moveTo(nX, nY);
			} else {
				ob.style.left = nX + 'px';
				ob.style.top = nY + 'px';
			}
		}
		if (mode=='measure')
			checkMeasure();
		//return false;
	}
}
function MU(e) {
	if (!e) e=event;
	if (mode=='measure') stopMeasure(e);
	if (ob) ob.style.zIndex=100;
	ds=true;
	//SD(ob);
	ob = null;
}
if (document.captureEvents) {
			document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
}
document.onmousedown = MD;
document.onmousemove = MM;
document.onmouseup = MU;
var detail;
function SD(obj) { //show detail
	if (detail) HD(null);
	if (ds && obj && (obj.className=='template' || obj.className=='instance')) {
		detail = document.createElement("div");
		detail.id='detail';
		detail.style.position='absolute';
		detail.style.top=0;
		detail.style.left=0;
		var text=document.createTextNode(describe(obj));
		detail.appendChild(text);
		obj.appendChild(detail);
	}
}
function HD(obj) {
	if (detail) {
		if (obj==null)
			obj = ((detail.parentElement) ? detail.parentElement : detail.parentNode);
		try {
			obj.removeChild(detail);
			detail=null;
		} catch (er) { }
	}
}
function describe(obj) {
	var t="";
	try {
		t+=O[obj.id].name + ' ';
		t+=obj.offsetHeight * pf + 'ft ';
		t+=obj.offsetWidth * pf + 'ft '
	} catch (er) { }
	return t;
}
var meas, im, mP0;
var mIs=new Array(new Image(0,0), new Image(0,0));
mIs[0].src='left.png';
mIs[1].src='right.png';
var mIn;
obs['M']=0;
function startMeasure() {
	mIn=1;
	//window.status='start';
	meas=document.createElement('div');
	im = document.createElement('img');
	im.src = mIs[mIn].src;
	meas.appendChild(im);
	meas.className='instance';
	obs['M']=obs['M']+1;
	id='M' + obs['M']
	meas.id=id;
	Ox=X;Oy=Y;Oz=1;Oh=0;Ow=0;
	meas.style.position='absolute';
	meas.style.top=Oy;
	meas.style.left=Ox;
	meas.style.height=Oh;
	meas.style.width=Ow;
	meas.style.border='none';
	meas.style.visible='hidden';
	document.body.appendChild(meas);
	var ds1=ds;
	if (!ds) {ds=true;}
	SD(meas);
	ds=ds1;
}
function checkMeasure() {
	//window.status='check';
	//if (meas.offsetWidth<0) {
	//	window.status='width';
	//	mIn=(mIn+1)%2;
	//	meas.style.left=meas.style.offsetLeft+meas.style.offsetWidth;
	//	meas.getElementsByTagName('img')[0].src=mIs[mIn].src;
	//}
	//if (meas.offsetHeight<0) {
	//	window.status='height';
	//	mIn=(mIn+1)%2;
	//	meas.style.top=meas.style.offsetTop+meas.style.offsetHeight;
	//	meas.getElementsByTagName('img')[0].src=mIs[mIn].src;
	//}
}
function stopMeasure(obj) {
	setMode('move');
	Fx=obj.clientX;
	Fy=obj.clientY;
	if (Fx<Ox) {
		mIn=(mIn+1)%2;
		meas.style.left=Fx;
		meas.style.width=Ox-Fx;
		meas.getElementsByTagName('img')[0].src=mIs[mIn].src;
	}
	if (Fy<Oy) {
		mIn=(mIn+1)%2;
		meas.style.top=Fy;
		meas.style.height=Oy-Fy;
		meas.getElementsByTagName('img')[0].src=mIs[mIn].src;
	}
	meas.style.visible='visible';
}

var DEFAULT_FPS=24;
var DEFAULT_START_O=100; //initial transparancy
var DEFAULT_STOP_O=0; //final transparancy
var DEFAULT_TIME=1000; //animation time in milliseconds
function fade(obj,finishAction,time,startO,stopO,fps) {
	fps = (fps) ? fps : DEFAULT_FPS;
	startO = (startO) ? startO : DEFAULT_START_O;
	stopO = (stopO) ? stopO : DEFAULT_STOP_O;
	time = (time) ? time : DEFAULT_TIME;

	if (!obj.fading) {
		obj.fading = true;
	}
	obj.style.filter='alpha(opacity='+ startO + ')';
	obj.style.MozOpacity = '' + startO/100;
	
	time -= 1000 / fps;
	var framesLeft = time / 1000 * fps;
	startO -= (startO-stopO)/framesLeft;
					
	if (time>0) {
		setTimeout(function(){fade(obj,finishAction,time,startO,stopO,fps)},1/fps*1000);
	} else {
		obj.fading = false;
		if (finishAction) {finishAction(obj);}
	}
}
function size(obj,finishAction,time,startW,startH,stopW,stopH,fps) {
	fps = (fps) ? fps : DEFAULT_FPS;
	time = (time) ? time : DEFAULT_TIME;
	
	obj.style.height=startH;
	obj.style.width=startW;
				   
	time -= 1000 / fps;
	var framesLeft = time / 1000 * fps;
	startH = Math.max(0,startH-(startH-stopH)/framesLeft);
	startW = Math.max(0,startW-(startW-stopW)/framesLeft);
					
	if (time>0) {
		setTimeout(function(){size(obj,finishAction,time,startW,startH,stopW,stopH,fps)},1/fps*1000);
	} else {
		obj.fading = false;
		if (finishAction) {finishAction(obj);}
	}
}
function move(obj,finishAction,time,startX,startY,stopX,stopY,fps,startZ,stopZ) {
	/*if (startX==stopX && startY==stopY)
		return*/
	obj.moving = true
	fps = (fps) ? fps : DEFAULT_FPS;
	time = (time) ? time : DEFAULT_TIME;

	obj.style.top = startY + "px";
	obj.style.left = startX + "px";

	time -= 1000 / fps;
	var framesLeft = time / 1000 * fps;
	startY = Math.floor(startY + (stopY-startY)/framesLeft)
	startX = Math.floor(startX + (stopX-startX)/framesLeft)
	
	if (time>0) {
		setTimeout(function(){move(obj,finishAction,time,startX,startY,stopX,stopY,fps,startZ,stopZ)},1/fps*1000);
	} else {
		obj.moving = null
		obj.style.top = stopY + "px"
		obj.style.left = stopX + "px"
		obj.fading = false
		if (finishAction) {finishAction(obj);}
	}
}